package org.yoki.edu.portal.core.radius.domain;

import com.fasterxml.jackson.annotation.JsonInclude;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yunzhitx.cloud.common.model.PageResult;
import com.yunzhitx.cloud.common.model.ddd.AbstractEntity;
import com.yunzhitx.cloud.common.model.ddd.InstanceFactory;
import com.yunzhitx.cloud.common.model.exception.BizException;
import lombok.Data;
import org.yoki.edu.portal.core.exception.BasicRestStatus;
import org.yoki.edu.portal.core.radius.infra.RadacctRepository;

import javax.persistence.*;
import java.io.Serializable;
import java.util.Date;

/**
 * 代码生成器自动生成
 * Date:2017-10-29 17:24:25
 *
 * @author
 */
@Data
@Table(name = "radacct")
@JsonInclude(JsonInclude.Include.NON_NULL)
public class Radacct implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private Integer id; //
    @Column(name = "acctsessionid")
    private String acctsessionid; //
    @Column(name = "acctuniqueid")
    private String acctuniqueid; //
    @Column(name = "username")
    private String username; //
    @Column(name = "groupname")
    private String groupname; //
    @Column(name = "realm")
    private String realm; //
    @Column(name = "nasipaddress")
    private String nasipaddress; //
    @Column(name = "nasportid")
    private String nasportid; //
    @Column(name = "nasporttype")
    private String nasporttype; //
    @Column(name = "acctstarttime")
    private Date acctstarttime; //
    @Column(name = "acctupdatetime")
    private Date acctupdatetime; //
    @Column(name = "acctstoptime")
    private Date acctstoptime; //
    @Column(name = "acctinterval")
    private Integer acctinterval; //
    @Column(name = "acctsessiontime")
    private Integer acctsessiontime; //
    @Column(name = "acctauthentic")
    private String acctauthentic; //
    @Column(name = "connectinfo_start")
    private String connectinfoStart; //
    @Column(name = "connectinfo_stop")
    private String connectinfoStop; //
    @Column(name = "acctinputoctets")
    private Long acctinputoctets; //
    @Column(name = "acctoutputoctets")
    private Long acctoutputoctets; //
    @Column(name = "calledstationid")
    private String calledstationid; //
    @Column(name = "callingstationid")
    private String callingstationid; //
    @Column(name = "acctterminatecause")
    private String acctterminatecause; //
    @Column(name = "servicetype")
    private String servicetype; //
    @Column(name = "framedprotocol")
    private String framedprotocol; //
    @Column(name = "framedipaddress")
    private String framedipaddress; //

    public Radacct() {
    }

    public Radacct(Integer id) {
        setId(id);
    }

    public Serializable getKey() {
        return getId();
    }

    public boolean existed() {
        return false;
    }


    public void save() throws Exception {
        if (existed()) {
            throw new BizException(BasicRestStatus.repeatDataErrorStatus);
        }
        getRadacctRepository().insert(this);
    }

    public static void delete(Integer[] ids) throws Exception {
        for (Integer id : ids) {
            getRadacctRepository().deleteByPrimaryKey(id);
        }
    }

    public void update() throws Exception {
        getRadacctRepository().updateByPrimaryKeySelective(this);
    }

    public Radacct detail() {
        return getRadacctRepository().selectByPrimaryKey(getKey());
    }

    public static PageResult<Radacct> page(PageResult<Radacct> page) {
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        PageInfo<Radacct> pageInfo = new PageInfo<>(getRadacctRepository().selectAll());
        page.setTotal(pageInfo.getTotal());
        page.setPageData(pageInfo.getList());

        return page;
    }

    private static RadacctRepository radacctRepository;

    public static RadacctRepository getRadacctRepository() {
        if (null == radacctRepository) {
            radacctRepository = InstanceFactory.getInstance(RadacctRepository.class);
        }
        return radacctRepository;
    }
}